package com.dslcode.cloud.common.utils;

import lombok.Data;

/**
 * AJAX请求的返回对象
 * @author 董思林
 * @date 2016-07-13
 */
@Data
public class AjaxResponse<T> {

    public static final int NO_TOKEN = 1102;
    public static final int INVALID_TOKEN = 3303;

    /** 响应状态  10000成功 20000失败 401未认证 402未授权 500服务器报错 */
    private int code;
    /** 是否操作成功 */
//    private boolean success;
    /** 操作说明信息 */
    private String msg;
    /** 响应数据 */
    private T result;

    public AjaxResponse(){}

    private AjaxResponse(int code, String msg, T result) {
        this.code = code;
        this.msg = msg;
        this.result = result;
    }

    /**
     * 自定义
     * @param code
     * @return
     */
    public static AjaxResponse build(int code) {
        return new AjaxResponse(code, null, null);
    }
    /**
     * 自定义
     * @param code
     * @param msg
     * @return
     */
    public static AjaxResponse build(int code, String msg) {
        return new AjaxResponse(code, msg, null);
    }
    /**
     * 自定义
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static AjaxResponse build(int code, String msg, Object data) {
        return new AjaxResponse(code, msg, data);
    }

    /**
     * 请求处理成功 code 10000
     * @return
     */
    public static AjaxResponse buildSuccess() {
        return new AjaxResponse(ResponseCode.SUCCESS.code(), null, null);
    }
    /**
     * 请求处理成功 code 10000
     * @param msg
     * @return
     */
    public static AjaxResponse buildSuccess(String msg) {
        return new AjaxResponse(ResponseCode.SUCCESS.code(), msg, null);
    }
    /**
     * 请求处理成功 code 10000
     * @param msg
     * @param data
     * @return
     */
    public static AjaxResponse buildSuccess(String msg, Object data) {
        return new AjaxResponse(ResponseCode.SUCCESS.code(), msg, data);
    }
    /**
     * 请求处理成功 code 10000
     * @param data
     * @return
     */
    public static AjaxResponse buildSuccessData(Object data) {
        return new AjaxResponse(ResponseCode.SUCCESS.code(), null, data);
    }

    /**
     * 请求处理失败(非系统原因) code 20000
     * @return
     */
    public static AjaxResponse buildFailed() {
        return new AjaxResponse(ResponseCode.FAIL.code(), null, null);
    }
    /**
     * 请求处理失败(非系统原因) code 20000
     * @param msg
     * @return
     */
    public static AjaxResponse buildFailed(String msg) {
        return new AjaxResponse(ResponseCode.FAIL.code(), msg, null);
    }
    /**
     * 请求处理失败(非系统原因) code 20000
     * @param msg
     * @param data
     * @return
     */
    public static AjaxResponse buildFailed(String msg, Object data) {
        return new AjaxResponse(ResponseCode.FAIL.code(), msg, data);
    }
    /**
     * 请求处理失败(非系统原因) code 20000
     * @param data
     * @return
     */
    public static AjaxResponse buildFailedData(Object data) {
        return new AjaxResponse(ResponseCode.FAIL.code(), null, data);
    }

    /**
     * 服务器内部错误 code 500
     * @return
     */
    public static AjaxResponse buildError() {
        return new AjaxResponse(500, null, null);
    }
    /**
     * 服务器内部错误 code 500
     * @param msg
     * @return
     */
    public static AjaxResponse buildError(String msg) {
        return new AjaxResponse(500, msg, null);
    }
    /**
     * 服务器内部错误 code 500
     * @param msg
     * @param data
     * @return
     */
    public static AjaxResponse buildError(String msg, Object data) {
        return new AjaxResponse(500, msg, data);
    }
    /**
     * 服务器内部错误 code 500
     * @param data
     * @return
     */
    public static AjaxResponse buildErrorData(Object data) {
        return new AjaxResponse(500, null, data);
    }

    /**
     * 没认证(登录) code 401
     * @return
     */
    public static AjaxResponse buildUnAuthenticated() {
        return new AjaxResponse(ResponseCode.UNAUTHORIZED.code(), "登录失效，请重新登录", null);
    }
    /**
     * 没认证(登录) code 401
     * @param msg
     * @return
     */
    public static AjaxResponse buildUnAuthenticated(String msg) {
        return new AjaxResponse(ResponseCode.UNAUTHORIZED.code(), msg, null);
    }

    /**
     * 没授权(没有权限访问) code 402
     * @return
     */
    public static AjaxResponse buildUnAuthorized() {
        return new AjaxResponse(ResponseCode.UNOPERATE.code(), "对不起，您没有权限访问", null);
    }
    /**
     * 没授权(没有权限访问) code 402
     * @param msg
     * @return
     */
    public static AjaxResponse buildUnauthorized(String msg) {
        return new AjaxResponse(ResponseCode.UNOPERATE.code(), msg, null);
    }


}